package org.feng.service;

import cn.hutool.core.util.StrUtil;
import org.feng.client.base.DictClient;
import org.feng.entity.CustomDict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 字典client实现
 *
 * @author feng
 */
@Service
public class DictClientImpl implements DictClient<Long, CustomDict> {

    @Resource
    private CustomDictService customDictService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addDict(CustomDict dict) {
        return customDictService.save(dict);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addDictList(List<CustomDict> dictList) {
        return customDictService.saveBatch(dictList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(CustomDict dict) {
        return customDictService.updateById(dict);
    }


    @Override
    public List<CustomDict> list(CustomDict customDict) {
        return customDictService.list(customDict);
    }

    @Override
    public List<CustomDict> listChildrenById(Long dictId) {
        return customDictService.listChildrenById(dictId);
    }

    @Override
    public CustomDict selectDict(CustomDict dict) {
        if(Objects.nonNull(dict.getId())) {
            return customDictService.getBaseMapper().selectById(dict.getId());
        }
        if(StrUtil.isNotBlank(dict.getDictKey())) {
            return customDictService.getByDictKey(dict.getDictKey());
        }
        throw new IllegalArgumentException("id 和 字典key 不能同时为空");
    }
}
